"""
角色管理
"""
from pymysqlutil import MysqlUtil


class RoleManager:
    def __init__(self):
        self.mysqlutil = MysqlUtil("db_qxgl")

    def submenu(self):
        menu = """
        1.查询
        2.修改
        3.添加
        4.删除
        0.退出
        """
        print(menu)

    def select(self):
        datas = self.mysqlutil.query_all("select * from tb_role")
        for data in datas:
            print(data)

    def insert(self):
        print("角色表展示：")
        self.select()
        sql = "insert into tb_role(rolename)values(%s)"
        role_name = input("请输入角色名称：")
        args = (role_name, )
        result = self.mysqlutil.operator_data(sql, args)
        if result == 1:
            print("添加成功")
        if result == 0:
            print("添加失败")

    def update(self):
        print("角色表展示：")
        self.select()
        rid = int(input("请输入要更改的角色id："))
        sql = f"select * from tb_role where id = {rid}"
        data = self.mysqlutil.query_one(sql)
        print(data)
        name = input("请输入更改后的名称：")
        sql = f"update tb_role set rolename='{name}' where id = {rid};"
        print(sql)
        result = self.mysqlutil.operator_data(sql)
        if result == 1:
            print("修改成功")
        if result == 0:
            print("修改失败")

    def delete(self):
        print("角色表展示：")
        self.select()
        id = int(input("请输入要删除的id："))
        sql = f"select * from tb_role where id = {id}"
        data = self.mysqlutil.query_one(sql)
        if data:
            sql = f"delete from tb_role where id = {id}"
            result = self.mysqlutil.operator_data(sql)
            if result == 1:
                print("删除成功")
            if result == 0:
                print("删除失败")
        else:
            print("您输入的id用户不存在！")

    def operation(self):
        while True:
            self.submenu()
            num = int(input("请输入你选择的功能数字："))
            if num == 1:
                print("查询")
                self.select()
            elif num == 2:
                print("修改")
                self.update()
            elif num == 3:
                print("添加")
                self.insert()
            elif num == 4:
                print("删除")
                self.delete()
            elif num == 0:
                print("退出")
                break
            else:
                print("输入错误，请重新输入：")
                continue
